package com.iot.inclinometer.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.iot.inclinometer.R;
import com.iot.inclinometer.util.ContextApplication;
/**
 * 欢迎界面
 * @author ZSL
 *
 */
public class WelcomeActivity extends Activity {
	private TextView productModelView;	   //产品型号显示
	private Button enterBtn;          	   //进入按钮
	
	private String model="产品型号：";    		   //产品型号
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0){
			   finish();
			   return;
			}
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.welcome_activity);
		SharedPreferences sharedPreferences=ContextApplication.getAppContext().getSharedPreferences("configuration",0);
//		SharedPreferences sharedPreferences=this.getSharedPreferences("configuration",0);
		model+=sharedPreferences.getString("Model","");
		initView();   	 	//初始化控件
		setBtnListener();	//设置监听
	}
	/*
	 * 初始化控件
	 * */
	private void initView() {
		productModelView = (TextView) findViewById(R.id.product_model_view);
		enterBtn = (Button) findViewById(R.id.enter_btn);
		productModelView.setText(model);
	}
	/*设置按钮的点击监听*/
	private void setBtnListener() {
		enterBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(getSharedPreferences("license",0).getString("KEY","").equals("right"))
				{	
					Intent intent = new Intent(WelcomeActivity.this,NewProUI.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					getApplicationContext().startActivity(intent);
					finish();
				}else
				{
					Intent intent = new Intent(WelcomeActivity.this,LicenseUI.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					getApplicationContext().startActivity(intent);
					finish();
				}
			}
		});	
	}

	/**
	 * 监听返回键
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			sureDialog();
			return true;
		}
		return true;
	}
	/**
	 * 确认框
	 */
	public void sureDialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setMessage("确定退出程序吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						android.os.Process.killProcess(android.os.Process
								.myPid());
					}
				});
		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}
}
